package com.ptit.piclient;

import java.io.ByteArrayOutputStream;
//import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
//import java.util.Random;

import com.ptit.piclient.R;
import com.ptit.piclient.model.Song;
//import com.ptit.piclient.tools.AddingSongsToPlaylist;
import com.ptit.piclient.tools.AllSongAdapter2;
import com.ptit.piclient.tools.PiConnector;
import com.ptit.piclient.tools.Utils;

import android.os.AsyncTask;
import android.os.Bundle;
//import android.os.StrictMode;
import android.app.Activity;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
//import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class AllSongActivity extends Activity implements OnItemClickListener, TextWatcher {
	ListView lv;
	EditText searchField ;
	ArrayList<Song> list = new ArrayList<Song>();
	@Override
	protected void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_song_list);
		
		// Search field and add its onChangeListener
		searchField = (EditText) findViewById(R.id.allSongSearchField);
		searchField.addTextChangedListener(this);
		
		lv = (ListView) findViewById(R.id.allSong);
		lv.setOnItemClickListener(this);
		
		new UpdateView(this).execute("");
		

	}
	
	
	/*
	 * New thread to update listview
	 */
	private class UpdateView extends AsyncTask<String,Void, AllSongAdapter2 > {
		Activity act;
		public UpdateView(Activity a){
			act = a;
		}
		@Override
		protected AllSongAdapter2 doInBackground(String... params) {
			// TODO Auto-generated method stub
			list = Utils.getInstance().getListByKeyword(params[0]);
			AllSongAdapter2 ad = new AllSongAdapter2(act, list);
			return ad;
		}
		
		@Override
	    protected void onPreExecute() {
			
		}
		protected void onPostExecute(AllSongAdapter2 result) {
			if (result != null)
				lv.setAdapter(result);			
		}
		
	}
	
	
	
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		// TODO Auto-generated method stub
		Song s = list.get(position);
		if (s != null) {
			try{
				new PlaySongAsyncTask().execute(s.getSongid());
			}catch(Exception ex){
				String err = (ex.getMessage()==null)?"Playsong from activity":ex.getMessage();
				Log.e("activity-playsong",err);
			}
			Log.i("nowplaying", "Moving...");

			Intent intent = new Intent(this, NowPlayingActivity.class);
			intent.putExtra("currentsong", AllSongActivity.serialize(s));
			startActivity(intent);

		}
	}
	/**
	 * Asynctast to play a song
	 */
	
	private class PlaySongAsyncTask extends AsyncTask<Integer, Void, Void>{

		@Override
		protected Void doInBackground(Integer... params) {
			// TODO Auto-generated method stub
			Integer i = params[0];
			int value = i.intValue();
			PiConnector.getInstance().playsong(value);
			return null;
		}
		
		
	}
	
	
	/**
	 * Method to convert song object into a byte array
	 * 
	 */
	public static byte[] serialize(Song obj) {
		ByteArrayOutputStream out = null;
		try {
			out = new ByteArrayOutputStream();
			ObjectOutputStream os = new ObjectOutputStream(out);
			os.writeObject(obj);
		} catch (Exception e) {
		}
		return out.toByteArray();
	}

	@Override
	public void afterTextChanged(Editable arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void beforeTextChanged(CharSequence s, int start, int count,
			int after) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {
		// TODO Auto-generated method stub
		new UpdateView(this).execute(s.toString());
	}
	
	
	
}